package com.example.apiinterface.controller;
import com.example.apiclientsdk.model.User;
import com.example.apiclientsdk.utils.SignUtils;

import com.example.apiinterface.common.ErrorCode;
import com.example.apiinterface.exception.BusinessException;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/name")
public class NameController {

    @GetMapping("/get")
    public String getNameByGet(String name,HttpServletRequest request) {
        String wsc = name;
        String wsc1 = request.getHeader("wsc");
        System.out.println(wsc1);
        return "你好"+wsc;
    }

    @PostMapping("/post")
    public String getNameByPost(@RequestParam String name) {
        return name;
    }

    @PostMapping("/user")
    public String getUsernameByPost(@RequestBody User user, HttpServletRequest request){
//        String accessKey = request.getHeader("accessKey");
//        String nonce = request.getHeader("nonce");
//        String timestamp = request.getHeader("timestamp");
//        String sign = request.getHeader("sign");
//        String body = request.getHeader("body");
//        // todo 实际情况应该是去数据库中查是否已分配给用户
//        if (!accessKey.equals("wsc")) {
//            throw new RuntimeException("无权限");
//        }
//        if (Long.parseLong(nonce) > 10000) {
//            throw new RuntimeException("无权限");
//        }
////        Long currentTime = System.currentTimeMillis() / 1000;
////        final Long ONE_MINUTES = 60 * 1L;
////        if ((currentTime - Long.parseLong(timestamp)) >= ONE_MINUTES) {
////            throw new RuntimeException("超时请求");
////        }
//
//        //todo 实际情况中是从数据库中查出 secretKey
//        String serverSign = SignUtils.genSign(body, "abc");
//        if (!sign.equals(serverSign)) {
//            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//        }
        String name = user.getName();
        return "用户名称为:"+user.getName();
    }

}
